package com.clouddo.admin.controller;

import com.clouddo.admin.domain.UserDO;
import com.clouddo.admin.dto.LoginDTO;
import com.clouddo.admin.service.LoginService;
import com.clouddo.admin.service.MenuService;
import com.clouddo.admin.service.TokenService;
import com.clouddo.admin.service.UserService;
import com.clouddo.admin.utils.MD5Utils;
import com.clouddo.admin.vo.LoginVO;
import com.clouddo.common.annotation.Log;
import com.clouddo.common.context.FilterContextHandler;
import com.clouddo.common.dto.UserToken;
import com.clouddo.common.utils.JwtUtils;
import com.clouddo.common.utils.R;
import com.clouddo.common.vo.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author bootdo 1992lcg@163.com
 * @version V1.0
 */
@Slf4j
@RestController
public class LoginController {
    @Autowired
    private MenuService menuService;
    @Autowired
    private LoginService loginService;

    /**
     * 用户登录
     * @param loginDTO
     * @return
     */
    @Log("登录")
    @PostMapping("/login")
    public JsonResult<LoginVO> login(@Valid @RequestBody LoginDTO loginDTO) {
        JsonResult<LoginVO> result = null;
        try {
            result = loginService.login(loginDTO);
        }catch (Exception e){
            result = JsonResult.getErrorResult("登录报错");
            log.error("登录报错:{}",e);
        }
        return result;
    }

    /**
     * 退出登录
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/logout")
    public JsonResult logout(HttpServletRequest request, HttpServletResponse response) {
        menuService.clearCache(Long.parseLong(FilterContextHandler.getUserID()));
        return JsonResult.getSuccessResult();
    }


}
